//
//  UpdateViewController.m
//  Cartoon
//
//  Created by dllo on 15/10/29.
//  Copyright © 2015年 YSZ. All rights reserved.
//

#import "UpdateViewController.h"
#import "UpdateModel.h"
#import "UpdateTableViewCell.h"
#import "CartoonDetailsViewController.h"
@interface UpdateViewController ()<UITableViewDataSource,UITableViewDelegate, UIWebViewDelegate>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *arr;
@property (nonatomic,strong) NSMutableArray *todayArr;
@property (nonatomic,strong) NSMutableArray *weekArr;
@property (nonatomic,strong) UISegmentedControl *seg;
@property (nonatomic,strong) UISegmentedControl *segplan;
@property (nonatomic,strong) UIWebView *webView;
@end

@implementation UpdateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self handleDataToday];
    [self handleDataThisWeek];
    UIView *segView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, 375, 40)];
    self.seg = [[UISegmentedControl alloc] initWithItems:@[@"今日更新",@"本周更新",@"更新计划"]];
    self.seg.frame = CGRectMake(20, 0, 335, 40);
    self.seg.selectedSegmentIndex = 0;
    [self.seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
    [segView addSubview:_seg];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SelfViewWidth, SelfViewHeight -64-49) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = NO;
    [self.view addSubview:self.tableView];
    self.tableView.tableHeaderView = segView;
    
    [_tableView registerClass:[UpdateTableViewCell class] forCellReuseIdentifier:@"Cell"];
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UpdateModel *updateModel = [self.arr objectAtIndex:indexPath.row];
    UpdateTableViewCell *cell = [UpdateTableViewCell updateCellWithTableView:tableView];
    cell.updateModel = updateModel;
    
    return cell;


}

// 今日更新
- (void)handleDataToday
{

    [NetHandler dataWithUrl:@"http://mobilev3.ac.qq.com/Update/updateTimeLineForIos/uin/0/local_version/3.1.0/channel/1001/guest_id/0DB20220-FBA8-4267-AA65-5DC78AFBCEB4/timeline_type/1/page/1/listcnt/20" complant:^(id data) {
        NSArray *dataArr = [data objectForKey:@"data"];
        self.todayArr = [NSMutableArray array];
        self.todayArr = [UpdateModel modelsWithArr:dataArr];
        [self segAction:_seg];
    }];


}

// 本周更新
- (void)handleDataThisWeek
{

    [NetHandler dataWithUrl:@"http://mobilev3.ac.qq.com/Update/updateTimeLineForIos/uin/0/local_version/3.1.0/channel/1001/guest_id/0DB20220-FBA8-4267-AA65-5DC78AFBCEB4/timeline_type/2/page/1/listcnt/20" complant:^(id data) {
        NSArray *dataArr = [data objectForKey:@"data"];
        self.weekArr = [NSMutableArray array];
        self.weekArr = [UpdateModel modelsWithArr:dataArr];
        [self segAction:_seg];
        
    }];

}

// 今日更新加载更多数据
- (void)loadMoreDataToday
{
    static NSInteger num = 2;
    [NetHandler dataWithUrl:[NSString stringWithFormat:@"http://mobilev3.ac.qq.com/Update/updateTimeLineForIos/uin/0/local_version/3.1.0/channel/1001/guest_id/0DB20220-FBA8-4267-AA65-5DC78AFBCEB4/timeline_type/1/page/%ld/listcnt/20 ",num++] complant:^(id data) {
        NSMutableArray *dataArr = [data objectForKey:@"data"];
        NSMutableArray *moreArr = [UpdateModel modelsWithArr:dataArr];
        self.arr = (NSMutableArray *)[self.arr arrayByAddingObjectsFromArray:moreArr];
        [_tableView reloadData];
        [self.tableView.footer endRefreshing];
        if (dataArr == nil) {
            NSLog(@"没有数据了");
            [self.tableView.footer endRefreshingWithNoMoreData];
        }
        
    }];
    
}


// 今日更新上拉加载
- (void)handleFooterRefreshToday
{
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        [self loadMoreDataToday];
        
    }];
    
}


// 本周更新加载更多数据
- (void)loadMoreDataWeek
{
    static NSInteger num = 2;
    [NetHandler dataWithUrl:[NSString stringWithFormat:@"http://mobilev3.ac.qq.com/Update/updateTimeLineForIos/uin/0/local_version/3.1.0/channel/1001/guest_id/0DB20220-FBA8-4267-AA65-5DC78AFBCEB4/timeline_type/2/page/%ld/listcnt/20 ",num++] complant:^(id data) {
        NSMutableArray *dataArr = [data objectForKey:@"data"];
        NSMutableArray *moreArr = [UpdateModel modelsWithArr:dataArr];
        self.arr = (NSMutableArray *)[self.arr arrayByAddingObjectsFromArray:moreArr];
        [_tableView reloadData];
        [self.tableView.footer endRefreshing];
        if (dataArr == nil) {
            NSLog(@"没有数据了");
            [self.tableView.footer endRefreshingWithNoMoreData];
        }
        
    }];
    
}


// 本周更新上拉加载
- (void)handleFooterRefreshWeek
{
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        [self loadMoreDataWeek];
        
    }];
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;

}

- (void)segAction:(UISegmentedControl *)seg
{
    self.arr = [NSMutableArray array];
    switch (seg.selectedSegmentIndex) {
        case 0:
            [self handleFooterRefreshToday];
            self.arr = self.todayArr;
            [self.tableView reloadData];
            break;
        case 1:
            [self handleFooterRefreshWeek];
            self.arr = self.weekArr;
            [self.tableView reloadData];
            break;
        case 2:
            [self addViewHandle];
            break;
            
        default:
            break;
    }
    
    
}


- (void)addViewHandle
{
    self.segplan = [[UISegmentedControl alloc] initWithItems:@[@"今日更新",@"本周更新",@"更新计划"]];
    self.segplan.frame = CGRectMake(20, 64, 335, 40);
    self.segplan.selectedSegmentIndex = 2;
    [self.segplan addTarget:self action:@selector(segWebAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.segplan];
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 104, SelfViewWidth, SelfViewHeight-153)];
    NSURLRequest *requestUrl = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://mobilev3.ac.qq.com/html5/timelist/wm-timelist.html"]];
    self.webView.delegate = self;
    [self.webView loadRequest:requestUrl];
    [self.view addSubview:self.webView];

}

- (void)segWebAction:(UISegmentedControl *)seg
{
    self.arr = [NSMutableArray array];
    switch (seg.selectedSegmentIndex) {
        case 0:
            _seg.selectedSegmentIndex = 0;
            [self segAction:_seg];
            self.tableView.hidden = NO;
            self.segplan.hidden = YES;
            self.webView.hidden = YES;
            break;
        case 1:
            _seg.selectedSegmentIndex = 1;
            [self segAction:_seg];
            self.tableView.hidden = NO;
            self.segplan.hidden = YES;
            self.webView.hidden = YES;
            break;
        case 2:
            self.tableView.hidden = YES;
            break;
            
        default:
            break;
    }
    
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    CartoonDetailsViewController *carDetail = [[CartoonDetailsViewController alloc] init];
    
    UpdateModel *updateModel = [self.arr objectAtIndex:indexPath.row];
    
    
    
    carDetail.idtext = updateModel.comic_id;
    
    [self.navigationController pushViewController:carDetail animated:YES];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    //将url转换为string
    NSString *requestString = [[request URL] absoluteString];
    NSArray *typeArray = [requestString componentsSeparatedByString:@"//"];
    if ([[typeArray firstObject] isEqualToString:@"http:"]) {
        
    }else{
//    NSArray *typeArray = [requestString componentsSeparatedByString:@"//"];
    
    NSString *str = [typeArray lastObject];
    
    CartoonDetailsViewController *carDetail = [[CartoonDetailsViewController alloc] init];
    
    carDetail.idtext = str;
//    NSArray *array = [requestString ];
    
    [self.navigationController pushViewController:carDetail animated:YES];
    
    NSLog(@"%@",requestString);
    }
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
